package com.house365.core.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.house365.core.R;
import com.house365.core.view.wheel.widget.OnWheelChangedListener;
import com.house365.core.view.wheel.widget.WheelAdapter;
import com.house365.core.view.wheel.widget.WheelView;
import com.house365.core.view.wheel.widget.adapters.ArrayWheelAdapter;
import com.house365.core.view.wheel.widget.adapters.WheelViewAdapter;
/**
 * @author py
 * @date 2012-10-12
 * @comment 滚轮选项对话框
 */
public class WheelDialog extends Dialog {
 
	private WheelView wheelView;
	public WheelDialog(Context context) {
		super(context);
	}

	public WheelDialog(Context context, int theme) {
		super(context, theme);
	} 

	
	public WheelView getWheelView() {
		return wheelView;
	}

	public void setWheelView(WheelView wheelView) {
		this.wheelView = wheelView;
	}

	public void setWheelAdapter(ArrayWheelAdapter wheelAdapter){
		//wheelView.setAdapter(wheelAdapter);
		wheelView.setViewAdapter(wheelAdapter);
	}
	public static class Builder {

		private Context context;
		private String title;
		private String message;
		private String positiveButtonText;
		private String negativeButtonText;
		private WheelDialog.OnClickListener positiveButtonClickListener,
				negativeButtonClickListener;
		private WheelViewAdapter wheelAdapter;
		public Builder(Context context) {
			this.context = context;
		} 

		/**
		 * Set the Dialog message from resource
		 * 
		 * @param title
		 * @return
		 */
		public Builder setMessage(int message) {
			this.message = (String) context.getText(message);
			return this;
		}

		/**
		 * Set the Dialog title from resource
		 * 
		 * @param title
		 * @return
		 */
		public Builder setTitle(int title) {
			this.title = (String) context.getText(title);
			return this;
		}

		/**
		 * Set the Dialog title from String
		 * 
		 * @param title
		 * @return
		 */
		public Builder setTitle(String title) {
			this.title = title;
			return this;
		}

		public Builder setWheelAdapter(WheelViewAdapter wheelAdapter){
			this.wheelAdapter = wheelAdapter;
			return this;
		}

		/**
		 * Set the positive button resource and it's listener
		 * 
		 * @param positiveButtonText
		 * @param listener
		 * @return
		 */
		public Builder setPositiveButton(int positiveButtonText,
				WheelDialog.OnClickListener listener) {
			this.positiveButtonText = (String) context
					.getText(positiveButtonText);
			this.positiveButtonClickListener = listener;
			return this;
		}

		/**
		 * Set the positive button text and it's listener
		 * 
		 * @param positiveButtonText
		 * @param listener
		 * @return
		 */
		public Builder setPositiveButton(String positiveButtonText,
				WheelDialog.OnClickListener listener) {
			this.positiveButtonText = positiveButtonText;
			this.positiveButtonClickListener = listener;
			return this;
		}

		/**
		 * Set the negative button resource and it's listener
		 * 
		 * @param negativeButtonText
		 * @param listener
		 * @return
		 */
		public Builder setNegativeButton(int negativeButtonText,
				WheelDialog.OnClickListener listener) {
			this.negativeButtonText = (String) context
					.getText(negativeButtonText);
			this.negativeButtonClickListener = listener;
			return this;
		}

		/**
		 * Set the negative button text and it's listener
		 * 
		 * @param negativeButtonText
		 * @param listener
		 * @return
		 */
		public Builder setNegativeButton(String negativeButtonText,
				WheelDialog.OnClickListener listener) {
			this.negativeButtonText = negativeButtonText;
			this.negativeButtonClickListener = listener;
			return this;
		}
		private int idx=-1;
		/**
		 * Create the custom dialog
		 */
		public WheelDialog create() {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			final WheelDialog dialog = new WheelDialog(context,
					R.style.dialog);
			//设置对话框属性
			Window dialogWindow = dialog.getWindow();
	        dialogWindow.setGravity(Gravity.CENTER | Gravity.BOTTOM);
	        
	        View contentlayout = inflater.inflate(R.layout.dialog_wheel, null);
	        dialog.addContentView(contentlayout, new LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
//	        LayoutParams layoutParams = contentlayout.getLayoutParams();
//	        if(layoutParams == null){
//	        	layoutParams =new LayoutParams(w, LayoutParams.WRAP_CONTENT);
//	        }
//	        contentlayout.setLayoutParams(layoutParams);
			
			//init wheel view
			WheelView wheelView = (WheelView)contentlayout.findViewById(R.id.wheelView);
			wheelView.setViewAdapter(wheelAdapter);
			wheelView.setVisibleItems(3);
			wheelView.setCurrentItem(0);
			wheelView.addChangingListener(new OnWheelChangedListener() {
				
				@Override
				public void onChanged(WheelView wheel, int oldValue, int newValue) {
					idx = newValue;
				}
			});
			((TextView) contentlayout.findViewById(R.id.dialog_title)).setText(title);
			if (positiveButtonText != null) {
				((Button) contentlayout.findViewById(R.id.dialog_button_ok))
						.setText(positiveButtonText);
				if (positiveButtonClickListener != null) {
					((Button) contentlayout.findViewById(R.id.dialog_button_ok))
							.setOnClickListener(new View.OnClickListener() {
								public void onClick(View v) {
									positiveButtonClickListener.onClick(dialog,
											idx);
								}
							});
				}
			} else {
				contentlayout.findViewById(R.id.dialog_button_ok).setVisibility(View.GONE);
			}
			// set the cancel button
			if (negativeButtonText != null) {
				((Button) contentlayout.findViewById(R.id.dialog_button_cancel))
						.setText(negativeButtonText);
				if (negativeButtonClickListener != null) {
					((Button) contentlayout.findViewById(R.id.dialog_button_cancel))
							.setOnClickListener(new View.OnClickListener() {
								public void onClick(View v) {
									negativeButtonClickListener.onClick(dialog,
											idx);
								}
							});
				}
			} else {
				// if no confirm button just set the visibility to GONE
				contentlayout.findViewById(R.id.dialog_button_cancel).setVisibility(View.GONE);
			}
			// set the content message
			if (message != null) {
				((TextView) contentlayout.findViewById(R.id.dialog_message))
						.setText(message);
			} 
		
			dialog.setContentView(contentlayout);
			return dialog;
		}

		  
	}

	public interface OnClickListener{
		public void onClick(WheelDialog dialog,int idx);
	}
}
